/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoexam.web;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.common.enums.OrderType;
import com.lczyfz.campusinfo.common.enums.PayType;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.utils.ApiResultUtils;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoServant;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoServantName;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoServantNameService;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoServantService;
import com.lczyfz.campusinfo.modules.campusinfoorder.service.CampusinfoOrderService;
import com.lczyfz.campusinfo.modules.campusinfopay.service.CampusinfoPayService;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 公务员考试报名Controller
 * @author zms
 * @version 2018-04-18
 */
@Controller
@RequestMapping(value = "api/campusinfoeducation/campusinfoServant")
public class CampusinfoApiServantController extends BaseController {

	@Autowired
	private CampusinfoServantService campusinfoServantService;
	@Autowired
	private CampusinfoServantNameService campusinfoServantNameService;
	@Autowired
	private CampusinfoOrderService campusinfoOrderService;
	@Autowired
	private CampusinfoPayService campusinfoPayService;
	@RequestMapping(value = "servantNameList")
	@ResponseBody
	public Map<String, Object> list(CampusinfoServantName campusinfoServantName, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		Page<CampusinfoServantName> page = campusinfoServantNameService.findPage(new Page<CampusinfoServantName>(request, response), campusinfoServantName);
		map.put("page", page.getList());
		map.put("pageNo", page.getPageNo());
		map.put("count", page.getCount());
		map.put("totalPage", page.getTotalPage());
		return map;
	}
	@RequestMapping(value = "findById")  //公务员详情
	@ResponseBody
	public Map<String, Object> getfindById(CampusinfoServantName campusinfoServantName, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		CampusinfoServantName page = campusinfoServantNameService.get(campusinfoServantName);
		map.put("page", page);
		return map;
	}
	@RequestMapping(value = "enlist")  //公务员报名
	@ResponseBody
	public Map<String, Object> enlist(CampusinfoServant campusinfoServant, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		if (campusinfoServant != null) {
			campusinfoServant.setUser(UserUtils.getUser());
			CampusinfoServant findcampusinfoServant  =campusinfoServantService.getByName(campusinfoServant);
			if (findcampusinfoServant==null) {
				campusinfoServant.setOrderCode(campusinfoOrderService.saveOrderCode(OrderType.Servan.getValue()));
				campusinfoServantService.save(campusinfoServant);
				CampusinfoServantName campusinfoServantName =campusinfoServantNameService.get(campusinfoServant.getServantId());
				String goodName = campusinfoServantName.getServantName()+ OrderType.Servan.getDesc();
				campusinfoPayService.ordersaveinsert(campusinfoServant.getOrderCode(),campusinfoServant.getPaymentMethod(),goodName,campusinfoServantName.getPayment()); //保存订单号
				campusinfoServantService.save(campusinfoServant);
				map.put("message", "报名成功");
			} else {
				findcampusinfoServant.setPaymentMethod(PayType.UNline.getValue());
				campusinfoServantService.save(findcampusinfoServant);
				map.put("message","用户已经报名");
			}
		} else {
			map.put("message", "报名失败");
		}
		return  map;
	}
	@RequestMapping(value = "addServant")  //线上公务员报名
	@ResponseBody
	public Map<String, Object> addCampusinfoServant(CampusinfoServant campusinfoServant, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		if (campusinfoServant != null) {
			campusinfoServant.setUser(UserUtils.getUser());
			CampusinfoServant findcampusinfoServant  =campusinfoServantService.getByName(campusinfoServant);
			if (findcampusinfoServant==null) {
				campusinfoServant.setOrderCode(campusinfoOrderService.saveOrderCode(OrderType.Servan.getValue()));
				campusinfoServantService.save(campusinfoServant);
				CampusinfoServantName campusinfoServantName =campusinfoServantNameService.get(campusinfoServant.getServantId());
				String goodName = campusinfoServantName.getServantName()+ OrderType.Servan.getDesc();
				campusinfoPayService.ordersaveinsert(campusinfoServant.getOrderCode(),campusinfoServant.getPaymentMethod(),goodName, campusinfoServantName.getPayment()); //保存订单号
				map.put("message", "报名成功");
				map.put("campusinfoServant",  campusinfoServant);
			} else if(findcampusinfoServant.getStatus().equals(CampusinfoOrderStat.UNDONE.getValue())){
				map.put("campusinfoServant",  findcampusinfoServant);
				map.put("message","用户已经报名但是未支付");
			} else {
				map.put("message","用户已经报名");
				map.put("success",false);
			}
		} else {
			map.put("message", "报名失败");
		}
		return  map;
	}

}